
import groovy.sql.Sql
import javax.servlet.http.HttpServletRequest

/**
 * mysql模板
 */
class MysqlTemplate {
    /**
     * 执行的方法
     * @param args 请求参数获取
     * @return 返回结果
     */
    static String runmysql(HttpServletRequest args) {
        //获取参数
        def data=args.getParameter("data")
        //初始化mysql连接 可参数传入
        def db_url="jdbc:mysql://localhost:3306/fault_flow?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"
        def username="root"
        def password="root"
        def driverClass="com.mysql.jdbc.Driver"
        def sql = Sql.newInstance(db_url, username, password, driverClass)
        //执行语句 可传入
        def count=0
        sql.eachRow("select * from fault"){row ->
            //打印日志
            println row.fault_id
            println row.fault_statu
            def con =row.fault_id
            def path = "D://asd"
            def pan = "E:"
            sql.execute("insert into testtable (id, name,code) values (${con}, ${path}, ${pan})")
            count++
        }
        //返回执行的结果
        return count
    }
}